package com.lxq.blog.com.lxq.blog.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.lxq.blog.com.lxq.blog.entity.Blog;
import com.lxq.blog.com.lxq.blog.service.BlogService;
import com.lxq.blog.com.lxq.blog.util.CommonUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@RestController
public class BlogController {

    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    BlogService blogService;

    @PostMapping("/user/up_blog_image")
    public String blogImageUp(MultipartFile blogImage) throws IOException {
        var imageFileUUID= UUID.randomUUID().toString().replace("-","");
        var filePath= CommonUtil.REPOSITORY_ROOT+imageFileUUID+".png";
        blogImage.transferTo(new File(filePath));
        JsonObject jsonObject=new JsonObject();
        jsonObject.addProperty("errno",0);
        JsonArray jsonArray=new JsonArray();
        JsonObject dataEle=new JsonObject();
        dataEle.addProperty("url","/repository/"+imageFileUUID+".png");
        jsonArray.add(dataEle);
        jsonObject.add("data",jsonArray);
//        System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }

    @GetMapping("/blogs")
    public ArrayList<Blog> findAll(){
        return blogService.findAll();
    }

    @GetMapping("/blog/{id}")
    public Blog findById(@PathVariable String id){
        blogService.viewUp(id);
        return blogService.findById(id);
    }

    @DeleteMapping("/user/blog/delete/{id}")
    public HashMap<String,Object> deleteById(@PathVariable String id){
        var hashMap=new HashMap<String,Object>();
        if (blogService.deleteById(id)){
            hashMap.put("result",true);
        }else{
            hashMap.put("result",false);
        }
        return hashMap;
    }

    @RequestMapping(value = "/blog/thumbUp/{id}",method = RequestMethod.PUT)
    public HashMap<String,Integer> thumbUp(@PathVariable String id){
        var thumb=blogService.thumbUp(id);
        var hashMap=new HashMap<String,Integer>();
        hashMap.put("code",200);
        hashMap.put("thumb",thumb);
        return hashMap;
    }

    @PostMapping("/user/publish_blog")
    public HashMap<String,Object> publishBlog(MultipartFile blogCover,HttpServletRequest request) throws IOException {
        var imageFileUUID= UUID.randomUUID().toString().replace("-","");
        var filePath= CommonUtil.REPOSITORY_ROOT+imageFileUUID+".png";
        blogCover.transferTo(new File(filePath));
        var blog=new Blog();
        blog.setCover(CommonUtil.SERVICE_REPOSITORY_ROOT+imageFileUUID+".png");
        var jsonNode = objectMapper.readTree(request.getParameter("blogContent"));
        blog.setTitle(jsonNode.path("title").asText());
        blog.setContent(jsonNode.path("content").asText());
        blog.setDescription(jsonNode.path("description").asText());
        blog.setId(UUID.randomUUID().toString().replace("-",""));
        blog.setDate(new SimpleDateFormat("YYYY-MM-DD HH:mm:ss")
                .format(new Date(System.currentTimeMillis())));
        blog.setUsername("lxq");

        var saveResult=blogService.saveBlog(blog);
        var hashResult=new HashMap<String,Object>();

        if (saveResult){
            hashResult.put("result","OK");
            hashResult.put("id",blog.getId());
        }else{
            hashResult.put("result","NO");
        }
        return hashResult;
    }

    @PostMapping("/user/blog/update")
    public HashMap<String,Object> updateBlog(@RequestBody HashMap<String,String> params) {
        var blog=new Blog();
        blog.setTitle(params.get("title"));
        blog.setContent(params.get("content"));
        blog.setDescription(params.get("description"));
        blog.setId(params.get("blogId"));

        var saveResult=blogService.updateById(blog);
        var hashResult=new HashMap<String,Object>();

        if (saveResult){
            hashResult.put("result","OK");
        }else{
            hashResult.put("result","NO");
        }
        return hashResult;
    }
}
